package edu.chl.da.ejb.ctrl;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import edu.chl.da.ejb.core.*;
import edu.chl.da.ejb.persistency.IHotelreservationFacade;
import javax.ejb.Stateless;
import javax.ejb.EJB;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;

/**
 * This class manages the hotel reservation communication with the hotel booking 
 * @author Jakob
 */
@Stateless
public class ManageHotel implements IManageHotel {

    @EJB
    IHotelreservationFacade hf;

    private final String URI = "http://localhost:8080/edu.chl.da._HotelBooking_war_1.0-SNAPSHOT/hotel/hotel";

    @Override
    public HotelSuggestion getFirstHotelSuggestion(Flight flight, String toDate) throws NullPointerException {
        
        if(flight == null) {
            throw new NullPointerException("Flight can not be null");
        }

        String dest = flight.getAirport().getDestination1().getName();
        String fromDate = flight.getStartdate();


        WebResource webResource = Client.create().resource(URI + "?destination="+dest+"&fromDate="+fromDate+"&toDate="+toDate);
        webResource.type(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML);
        ClientResponse response = webResource.get(ClientResponse.class);

        HotelSuggestion hotelSuggestion = response.getEntity(HotelSuggestion.class);

        return hotelSuggestion;
    }

    @Override
    public HotelSuggestion getNextHotelSuggestion(HotelSuggestion oldSuggestion) throws NullPointerException {
        if(oldSuggestion == null) {
            throw new NullPointerException("The old hotel suggestion can not be null");
        }

        WebResource webResource = Client.create().resource(oldSuggestion.getNext());
        webResource.type(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML);
        ClientResponse response = webResource.get(ClientResponse.class);

        HotelSuggestion hotelSuggestion = response.getEntity(HotelSuggestion.class);

        return hotelSuggestion;


    }

    @Override
    public HotelSuggestion getPreviousHotelSuggestion(HotelSuggestion oldSuggestion) {

        if(oldSuggestion == null) {
            throw new NullPointerException("The old hotel suggestion can not be null");
        }

        WebResource webResource = Client.create().resource(oldSuggestion.getPrevious());
        webResource.type(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML);
        ClientResponse response = webResource.get(ClientResponse.class);

        HotelSuggestion hotelSuggestion = response.getEntity(HotelSuggestion.class);

        return hotelSuggestion;
    }

    @Override
    public Hotelreservation bookSuggestion(HotelSuggestion hotelSuggestion, Reservation reservation) throws NullPointerException {

        if(hotelSuggestion == null) {
            throw new NullPointerException("The hotel suggestion can not be null");
        }
        else if(reservation == null) {
            throw new NullPointerException("The reservation can not be null");
        }

        // Sends the post and gets the reservation nr in result
        WebResource webResource = Client.create().resource(URI);
        webResource.type(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML);
        int i = webResource.post(Integer.class, hotelSuggestion);

        // Creates a new hotel reservation key
        HotelreservationPK hrPK = new HotelreservationPK();
        hrPK.setAirplane(reservation.getReservationPK().getAirplane());
        hrPK.setBookingnumber(i);
        hrPK.setCustomer(reservation.getReservationPK().getCustomer());
        hrPK.setFromairport(reservation.getReservationPK().getFromairport());
        hrPK.setFromdestination(reservation.getReservationPK().getFromdestination());
        hrPK.setSeatno(reservation.getReservationPK().getSeatno());
        hrPK.setSeatrow(reservation.getReservationPK().getSeatrow());

        // Creates a new hotel reservation
        Hotelreservation hr = new Hotelreservation();
        hr.setHotelreservationPK(hrPK);
        hr.setReservation(reservation);

        // Saves the reservation in databas
        hf.create(hr);

        return hr;
    }
 
}
